<blockquote class="layui-elem-quote">分类列表</blockquote>

<table id="category-list" lay-filter="category-list"></table>

<!--行工具条-->
<script type="text.html" id="category-bar">
<a class="layui-btn layui-btn-sm layui-btn" lay-event="edit">
<i class="layui-icon layui-icon-edit"></i>
编辑</a>
<a class="layui-btn layui-btn-sm layui-btn-normal" lay-event="del">
<i class="layui-icon layui-icon-close"></i>
删除</a>
</script>
<script>
    layui.use(['table'],function (){
        var table = layui.table;
        $ = layui.$;
        //ajax获取数据  渲染到表格
        table.render({
            elem:'#category-list',
            url:'/api/category',//获取分类信息
            page:true, //开启分页
            cols:[[
               //每一个列如何渲染
                {field:'id',title:'ID',},
                {field: 'name',title: '分类名',},
                {field: 'dscp',title: '描述',},
                {field: 'pic',title: '图片',templet: function (d){
                    if (d.pic){
                        return '<img src="/upload/'+d.pic+'">';
                    }else {
                        return '';
                    }
                    }},
                {field: 'parentId',title: '父分类',templet: function (d){
                    return (d.parent)? d.parent.name : '';
                    },},
                {field: 'recom',title: '主页推荐',templet: function (d){
                    return ['不推荐','推荐'][d.recom];
                    },},
                {field: 'status',title: '状态',templet: function (d){
                    return ['上架','下架'][d.status];
                    },},
                {title: '操作', toolbar: '#category-bar'},
            ]],
            //数据解析
            parseData: function (d){
              return{
                  code: d.code,
                  msg: d.msg,
                  count: d.data.total,
                  data: d.data.list,
              }  ;
            },
        });

        //行工具条的点击操作
        table.on('tool(category-list)',function (obj){
            // layer.msg('行工具事件')
            if (obj.event == 'edit'){
                    // layer.msg('编辑');
                //向新页面传送数据
                sessionStorage.setItem('category-edit',JSON.stringify(obj.data));


                //弹出层
                layer.open({
                    type:2,
                    title:'编辑分类',
                    area:['500px','500px'],
                    content:'/category/edit.html', //页面的url
                    end:function (){
                        //弹出层关闭  刷新表格
                        table.reload('category.list');
                    }
                });
            }else if(obj.event == 'del') {
                // layer.msg('删除');
                layer.confirm('确定要删除当前记录吗?',function (index){
                    //关闭弹出框
                    layer.close(index);
                    //点击确定时的操作
                    //ajax   /api/category  delete，  普通参数传ID
                    $.ajax({
                       url: '/api/category',
                       data: {id: obj.data.id},
                       type: 'delete',
                       dataType: 'json',
                       success: function (result){
                        if (result.code==0){
                            layer.msg('删除成功');

                        }else {
                            layer.msg(result.msg);
                        }

                        //刷新表格表格
                        table.reload('category-list');

                       },
                       error:function (){
                           layer.msg('请求失败');
                       }
                    });
                });
            }
        });

    });
</script>